package com.cheer.ba.view.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.cache.preference.Session;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.H5Address;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.model.AskGetMoneyModel;
import com.cheer.ba.presenter.impl.AskQuestPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.ShareNewsUtil;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;
import com.umeng.socialize.UMShareAPI;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: hq
 * Email: 834138841@qq.com
 * Comment: //TODO 邀请好友返利
 * Date: 2017-07-28 11:30
 */
public class RequestFriendAty extends BaseCommAty {

    //可提现的佣金
    @BindView(R.id.id_request_money_txt)
    TextView mTxtGetMoney;
    private String mCanGetMoney;
    //邀请好友的人数
    @BindView(R.id.id_request_person_num_txt)
    TextView mTxtPersonNum;
    //佣金总额
    @BindView(R.id.id_request_total_money_txt)
    TextView mTxtTotalMoney;

    private ShareNewsUtil mShare;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_request_friend, null, false));
    }

    @Override
    public boolean hasDataInPage() {
        return true;
    }

    @Override
    public void initPresenter() {
        presenter = new AskQuestPresenter();
    }

    @Override
    public void initView() {
        setTitleText("邀请返利");
        mTxtGetMoney.setText("0元");
        mTxtPersonNum.setText("0人");
        mTxtTotalMoney.setText("0元");
        ((AskQuestPresenter) presenter).getAskMoney(false);
        EventBus.getDefault().register(this);
    }

    @OnClick({R.id.id_request_get_money_txt, R.id.id_look_prize_linear, R.id.id_request_friend_btn, R.id.id_had_request_friend_linear, R.id.id_get_total_money_linear})
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_request_get_money_txt:
                //申请体现
                if (StringUtils.isEmpty(mCanGetMoney) || Double.parseDouble(mCanGetMoney) <= 0) {
                    SVProgressHUD.showInfoWithStatus(aty, "您还没有可提现的金额");
                } else {
                    Intent intent = new Intent(aty, AskGetMoneyAty.class);
                    intent.putExtra(IntentKey.General.KEY_PAY_MONEY, mCanGetMoney);
                    showActivity(aty, intent);
                }
                break;
            case R.id.id_look_prize_linear:
                //查看奖励说明
                CommH5WithTitleAty.startCommonH5(aty, H5Address.LOOK_PRIZE_SAID, "奖励说明", false);
                break;
            case R.id.id_request_friend_btn:
                //邀请好友
                String url = H5Address.H5_HEAD + "wx/active/v1/activeregister?custNo=" + Session.getUserNo();
                mShare = new ShareNewsUtil(aty, "88元红包，注册就送！", "加油省钱？选加油啦错不了！", url, "");
                mShare.showShare();
                break;
            case R.id.id_had_request_friend_linear:
                //查看邀请的好友
                showActivity(aty, MyRequestAllFriendAty.class);
                break;
            case R.id.id_get_total_money_linear:
                //查看我的佣金总额

                break;
        }
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_ASK_MONEY)) {
                //获得邀请返利成功
                final AskGetMoneyModel model = (AskGetMoneyModel) t;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (null != model) {
                            mCanGetMoney = model.getWithdraw();
                            mTxtGetMoney.setText(Currency.returnDollar(mCanGetMoney).replace("元", ""));
                            mTxtPersonNum.setText(model.getInvitationCount() + "人");
                            mTxtTotalMoney.setText(Currency.returnDollar(model.getProfit()));
                        }
                    }
                });
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        //返回类型
        String type = event.type;
        if (TextUtils.equals(type, ConstantTag.GET_MONEY_SUCCESS)) {
            //提现成功
            ((AskQuestPresenter) presenter).getAskMoney(false);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        /** attention to this below ,must add this**/
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
